<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
        }

        .navbar {
            width: 100%;
            height: 60px;
            background-color: #333;
            display: flex;
            align-items: center;
            padding: 0 20px;
            color: white;
        }

        .menu {
            position: relative;
            margin-left: auto; /* 将菜单放在右侧 */
        }

        .title {
            cursor: pointer;
            padding: 10px 20px;
            background-color: #444;
            color: white;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }
        .title:hover {
            background-color: #555;
        }

        .list {
            list-style: none;
            position: absolute;
            top: 60px; /* 下拉菜单紧贴标题栏 */
            right: 0;
            display: none;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
            z-index: 1000;
        }

        .list li {
            padding: 10px 20px;
            color: #333;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .list li:hover {
            background-color: #f0f0f0;
        }
    </style>
</head>

<body>
    <div class="navbar">
        <span>我的网站</span>
        <div class="menu">
            <div class="title">菜单</div>
            <ul class="list">
                <li>功能1</li>
                <li>功能2</li>
                <li>功能3</li>
                <li>功能4</li>
                <li>功能5</li>
            </ul>
        </div>
    </div>

    <script>
        var nav = document.querySelectorAll('li');
        var tit = document.querySelector('.title');
        var list = document.querySelector('.list');
        tit.onclick = function(){
            if(list.style.display=='block'){
                list.style.display ='none';
            }else{
                list.style.display='block';
            }
        }
        // 点击菜单项更新标题并隐藏菜单
        nav.forEach(function (item, key) {
            item.onclick = function () {
                tit.innerText = nav[key].innerText;
                list.style.display = 'none';
            };
        });

        // 点击页面其他地方隐藏菜单
        document.addEventListener('click', function (e) {
            if (!e.target.closest('.menu')) {
                list.style.display = 'none';
            }
        });
    </script>
</body>

</html>